import React from 'react';

const useFormChange = form => {
    const [formValue, setFormValue] = React.useState(null);

    const initialForm = () => {
        setTimeout(() => {
            setFormValue({ ...form?.getFieldsValue() });
        }, 0);
    };

    const hasFormChange = () => {
        let flag = false;
        const formValueNew = form?.getFieldsValue() || {};
        // eslint-disable-next-line no-restricted-syntax
        for (const o of Object.entries(formValue)) {
            const [key, value] = o;
            if (JSON.stringify(formValueNew[key]) !== JSON.stringify(value)) {
                flag = true;
                break;
            }
        }
        return flag;
    };

    return {
        initialForm,
        hasFormChange
    };
};

export default useFormChange;
